package com.sbdo.config;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.sbdo.service.DemoService;
import com.sbdo.service.impl.DemoServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableDubbo(scanBasePackages = "com.sbdo")
public class ProvideConfig {

    /**
     *   <dubbo:application name="gmall-user-provider" />
     */
    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("prodiver-a");
        return applicationConfig;
    }

    /**
     * providerconfig
     */
    @Bean
    public ProviderConfig providerConfig(){
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setWait(3000);
        providerConfig.setPayload(128 * 1024 * 1024);
        providerConfig.setThreads(400);

        return providerConfig;
    }

    /**
     * 推荐在provider上尽量多配置consumer端属性，consumer会使用provider配置的值作为缺省值
     */
    @Bean
    public ConsumerConfig consumerConfig(){
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(20000);
        consumerConfig.setCheck(false);
        consumerConfig.setRetries(0);

        return consumerConfig;
    }

    /**
     * <dubbo:registry address="zookeeper://106.15.176.xxx:2181?backup=106.15.176.xxx:2182,106.15.176.xxx:2183" />
     */
    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("127.0.0.1:2181");

        return registryConfig;
    }

    /**
     * <dubbo:protocol name="dubbo" port="20880" />
     */
       @Bean
    public ProtocolConfig protocolConfig(){
           ProtocolConfig protocolConfig = new ProtocolConfig();
           protocolConfig.setPort(20880);
           protocolConfig.setName("dubbo");
           protocolConfig.setThreads(100);

           return protocolConfig;
       }

    /**
     * 服务提供接口
     *
     * @param  demoService 入参的userService<br>
     *                    是从ioc中拿去 @Component public class UserServiceImpl implements UserService {...}
     * @return
     */
   /* @Bean
    public ServiceConfig<DemoService> userServiceServiceConfig(DemoServiceImpl demoService) {
        ServiceConfig<DemoService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(DemoService.class);
        serviceConfig.setRef(demoService);
        serviceConfig.setVersion("1.0.0");
        serviceConfig.setTimeout(3000);
        serviceConfig.setRetries(3);

        //配置每一个method的属性
        MethodConfig methodConfig = new MethodConfig();
        methodConfig.setName("sayHello");
        methodConfig.setTimeout(3000);
        methodConfig.setRetries(2);

        //将method的设置关联到service的配置中
        List<MethodConfig> methods = new ArrayList();
        methods.add(methodConfig);
        serviceConfig.setMethods(methods);

        return serviceConfig;
    }*/

}